package com.ttmall.service.impl;

import com.ttmall.common.pojo.EasyUITreeNode;
import com.ttmall.common.pojo.TaotaoResult;
import com.ttmall.mapper.TbContentCategoryMapper;
import com.ttmall.pojo.TbContentCategory;
import com.ttmall.pojo.TbContentCategoryExample;
import com.ttmall.service.ContentCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 内容分类管理 service
 * Created by GalaIO on 2018/1/17.
 */
@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {

    @Autowired
    private TbContentCategoryMapper contentCategoryMapper;

    @Override
    public List<EasyUITreeNode> listContentCategory(Long parentId) {

        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbContentCategory> list = contentCategoryMapper.selectByExample(example);

        List<EasyUITreeNode> result = new ArrayList<>();
        for (TbContentCategory contentCategory : list) {
            result.add(EasyUITreeNode.build(contentCategory.getId(),
                contentCategory.getName(),
                contentCategory.getIsParent() ? "closed" : "open"));
        }

        return result;
    }

    @Override
    public TaotaoResult createContentCat(Long parentId, String name) {
        Date now = new Date();
        TbContentCategory contentCategory = new TbContentCategory();
        contentCategory.setParentId(parentId);
        contentCategory.setName(name);
        contentCategory.setStatus(1);
        contentCategory.setIsParent(false);
        //排列序号，表示同级类目的展现次序，如数值相等则按名称次序排列。取值范围:大于零的整数'
        contentCategory.setSortOrder(1);
        contentCategory.setCreated(now);
        contentCategory.setUpdated(now);
        contentCategoryMapper.insert(contentCategory);

        //判断父节点更新，如果是叶节点变为父节点
        TbContentCategory parentNode = contentCategoryMapper.selectByPrimaryKey(parentId);
        if (parentNode != null && parentNode.getIsParent() == false) {
            parentNode.setIsParent(true);
            contentCategoryMapper.updateByPrimaryKeySelective(parentNode);
        }
        return TaotaoResult.ok(contentCategory);
    }

    @Override
    public TaotaoResult updateContentCat(Long id, String name) {
        TbContentCategory contentCategory = new TbContentCategory();
        contentCategory.setId(id);
        contentCategory.setName(name);
        contentCategoryMapper.updateByPrimaryKeySelective(contentCategory);
        return TaotaoResult.ok();
    }

    @Override
    public TaotaoResult deleteContentCat(Long id) {
        List<Long> parentIds = new ArrayList<>();
        queryContentCatSubParentNode(id, parentIds);
        parentIds.add(id);
        contentCategoryMapper.deleteAllParentAndChildren(parentIds);
        return TaotaoResult.ok();
    }

    private void queryContentCatSubParentNode(long id, List<Long> parentIds) {
        if (parentIds == null) {
            return;
        }
        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(id);
        criteria.andIsParentEqualTo(true);
        List<TbContentCategory> list = contentCategoryMapper.selectByExample(example);
        for (TbContentCategory item : list) {
            parentIds.add(item.getId());
            queryContentCatSubParentNode(item.getId(), parentIds);
        }
    }
}
